<template>
  <div class="order-header">
    <div class="container clearfix">
      <div class="header-logo">
        <a href="/#/index"></a>
      </div>
      <div class="title">
        <h2>{{ title }}
          <slot name="tip"></slot>
        </h2>
      </div>
      <div class="username">
        <el-dropdown @command="handleCommand">
        <span>
          <a href="javascript:;">{{ username }}</a>
        </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="center">个人中心</el-dropdown-item>
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>
<script>
import {mapState} from 'vuex'
import {logout} from "@/api/user";

export default {
  name: 'OrderHeader',
  props: {
    title: String
  },
  computed: {
    ...mapState(['username'])
  },
  methods: {
    handleCommand(command) {
      if(command === 'logout') {
        logout().then(() => {
          this.$message.success('退出登录成功')
          this.$cookies.remove("id")
          this.$cookies.remove("mallUserName")
          this.$store.dispatch('saveUserName',null);
          this.$store.dispatch('saveCartCount',0);
          this.$router.push('/index')
        })
      }
    }
  }
}
</script>
<style lang="scss">
.order-header {
  padding: 30px 0;
  border-bottom: 2px solid #FF6600;

  .header-logo {
    float: left;
    width: 62px;

    a {
      display: block;
      width: 56px;
      height: 56px;
      background: url("~@/assets/imgs/logo-mi.png") no-repeat 100%;
      background-size: contain;
    }
  }

  .title, .username {
    display: inline-block;
    height: 55px;
    line-height: 55px;
  }

  .title {
    float: left;
    margin-left: 54px;

    h2 {
      font-size: 28px;
      color: #333333;
    }

    span {
      font-size: 14px;
      margin-left: 17px;
      color: #999999;
      font-weight: 200;
    }
  }

  .username {
    float: right;

    a {
      color: #666666;
      font-size: 16px;
    }
  }
}
</style>
